package com.zl.service.impl;

import com.zl.dao.EmpDao;
import com.zl.pojo.Emp;
import com.zl.pojo.FenYe;
import com.zl.pojo.Query;
import com.zl.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpDao ed;
    @Override
    public List<Emp> queryEmpByLike(Query query) {
        List<Emp> emps=ed.queryEmpByLike(query);
        return emps;
    }

    @Override
    public int deleteEmp(int[] empNos) {
        /**
         * 理论上必须提供事务
         */
        if(empNos!=null && empNos.length>0){
            for(int empNo:empNos){
                ed.deleteEmp(new Integer(empNo));
            }
        }
        return 1;
    }

    @Override
    public List<Emp> queryAllMgrEmp() {
        List<Emp> magrs=ed.queryAllMgrEmp();
        return magrs;
    }

    @Override
    public int addEmp(Emp emp) {
        int flag=ed.addEmp(emp);
        return flag;
    }

    @Override
    public Emp queryEmpByEmpNo(int empNo) {
        Emp e=ed.queryEmpByEmpNo(empNo);
        return e;
    }

    @Override
    public int updateEmp(Emp emp) {
        int flag=ed.updateEmp(emp);
        return flag;
    }

    @Override
    public List<Emp> queryAllEmpByFenYe(FenYe fenYe) {
        //查询符合要求的记录总数
        int num=queryRowsCountByQuery(fenYe.getQuery());
        fenYe.setRowsCount(num);
        //处理页码
        if(fenYe.getPage()==null || fenYe.getPage()==0){
            fenYe.setPage(1);
        }else{
            if(fenYe.getPage()<1){
                fenYe.setPage(1);
            }
            if(fenYe.getPage()>fenYe.getPagesCount()){
                fenYe.setPage(fenYe.getPagesCount());
            }
        }
        List<Emp> emps =ed.queryAllEmpByFenYe(fenYe);
        return emps;
    }

    @Override
    public int queryRowsCountByQuery(Query query) {
        int num=ed.queryRowsCountByQuery(query);
        return num;
    }
}
